/**
 * 
 */
package com.java.controller.front;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.java.entity.Classroom;
import com.java.entity.Teacher;
import com.java.entity.Users;
import com.java.service.impl.ClassroomServiceImpl;
import com.java.service.impl.CollegeServiceImpl;
import com.java.service.impl.CourseServiceImpl;
import com.java.service.impl.TeacherSeriveImpl;
import com.java.utils.CookieUtils;

/**
* @author Kidney
* 创建时间：2019年4月30日 下午11:44:49
* Description:
*/
/**
 * @author KIDNEY
 *
 */
@Controller
public class ClassroomController {

	@Autowired
	private CourseServiceImpl cs;

	@Autowired
	private CollegeServiceImpl cos;

	@Autowired
	private TeacherSeriveImpl ts;

	@Autowired
	private ClassroomServiceImpl cls;

	@RequestMapping("book_classroom")
	public String bookUser() {
		return "pages/user/user_classroom.jsp";
	}

	@RequestMapping("admin_classroom")
	public String classAdmin() {
		return "pages/admin/classroomHome.jsp";
	}

	@RequestMapping(value = "book_class", produces = "application/json; charset=utf-8")
	public String classBook(Model model, Integer classid, Integer time, String weekday,
			String classUse, HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();

		Users users = (Users) subject.getPrincipal();

		Integer userid = Integer.valueOf(users.getUsername());
		int flag = cls.insert(userid, classid, classUse, weekday, time);
		
		if (flag == 1) {
			request.setAttribute("msg", "booking成功");
		} else {

			request.setAttribute("msg", "bookingfail");
		}

		return "pages/user/user_classroom.jsp";
	}

	@RequestMapping("class_pass")
	public String classHomeAdmin(Model model, Integer id, HttpServletRequest request) {
		int flag = cls.pass(id);
		if(flag==1) {
		request.setAttribute("msg","ORDER ID:"+ id + "pass成功");
		}else {request.setAttribute("msg","ORDER ID:"+ id + "pass fail");
			
		}
		return "pages/admin/classroomHome.jsp";
	}

	/**
	 * all booking oders by admin
	 * 
	 * @return
	 */
	@RequestMapping(value = "all_bookings", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String bookingOrders() {
		List<Classroom> list=	cls.selectAll();
		
		String jsonString = JSON.toJSONString(list);
		return jsonString;
	}

	@RequestMapping(value = "classid_bookings", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String classidbooking(Integer classid) {
		List<Classroom> list= cls.selectByClassid(classid);
		

		String jsonString = JSON.toJSONString(list);
		return jsonString;
	}
	
	
	@RequestMapping(value = "user_bookings", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String userSelfBookings(HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();
		Users users = (Users) subject.getPrincipal();

		Integer userid = Integer.valueOf(users.getUsername());
		List<Classroom> list= cls.selectByUserid(userid);
		

		String jsonString = JSON.toJSONString(list);
		return jsonString;
	}

	@RequestMapping(value = "delete_booking", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Integer deleteBooking(Integer id) {

		return cls.delete(id);
	}

}
